package org.dandsoft.blackbox.labs.snn;

import org.dandsoft.blackbox.snn.NeuronStructure;
import org.dandsoft.blackbox.snn.SnnException;
import org.dandsoft.blackbox.snn.Neuron;
import org.dandsoft.blackbox.snn.Synapse;
import org.dandsoft.blackbox.ObjectId;
import org.dandsoft.blackbox.IntegerObjectId;

import java.util.Iterator;

/**
 * User: dvilyuzhanin
 * Date: 23.01.2009 0:38:52
 */
@Deprecated
public class NeuronLayerGenerator {

    private int[] sourceNeurons;
    private int[][] synapses;
    private int[] destinationNeurons;
    private int sourceNeuronsCount;
    private int destinationNeuronCount;
    private int synapsesCount;
    private int sourceNeuronSynapsesCount;
    private int destinationNeuronSynapsesCount;

    public synchronized void generate(LoadingModel model) {
        sourceNeuronsCount = model.getSourceNeuronsCount();
        destinationNeuronCount = model.countDestinationNeurons();
        synapsesCount = model.countSynapses();

        sourceNeurons = new int[sourceNeuronsCount];
        destinationNeurons = new int[destinationNeuronCount];
        synapses = new int[synapsesCount][2];
        for (int i = 0; i < synapses.length; i++) {
            for (int j = 0; j < synapses[i].length; j++) {
                synapses[i][j] = -1;
            }
        }
        sourceNeuronSynapsesCount = model.countSourceNeuronSynapses();
        destinationNeuronSynapsesCount = model.countDestinationNeuronSynapses();

        int destinationIndex = -1;
        int synapseIndex = -1;
        for (int sourceIndex = 0; sourceIndex < sourceNeurons.length; sourceIndex++) {
            for (int i = 0; i < sourceNeuronSynapsesCount; i++) {
                destinationIndex = findDestinationNeuron(destinationIndex);
                synapseIndex++;

                if (synapseIndex < synapsesCount) {
                    sourceNeurons[sourceIndex]++;
                    destinationNeurons[destinationIndex]++;
                    synapses[synapseIndex][0] = sourceIndex;
                    synapses[synapseIndex][1] = destinationIndex;
                } else {
                    return;
                }
            }
        }
    }


    private int findDestinationNeuron(int previousIndex) {
        int index = previousIndex;
        while (true) {
            index++;
            if (index >= destinationNeurons.length) {
                index = -1;
                continue;
            } else if (destinationNeurons[index] > destinationNeuronSynapsesCount) {
                continue;
            }
            return index;
        }
    }


    public synchronized int[] getSourceNeurons() {
        return sourceNeurons;
    }

    public synchronized int[][] getSynapses() {
        return synapses;
    }

    public synchronized int[] getDestinationNeurons() {
        return destinationNeurons;
    }

    public synchronized int getSourceNeuronsCount() {
        return sourceNeuronsCount;
    }

    public synchronized int getDestinationNeuronCount() {
        return destinationNeuronCount;
    }

    public synchronized int getSynapsesCount() {
        return synapsesCount;
    }

    public synchronized int getSourceNeuronSynapsesCount() {
        return sourceNeuronSynapsesCount;
    }

    public synchronized int getDestinationNeuronSynapsesCount() {
        return destinationNeuronSynapsesCount;
    }
}
